package com.screen.screendisplay.service.impl.coal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.screen.screendisplay.entity.coal.CoalProdoperTransport;
import com.screen.screendisplay.mapper.coal.CoalProdoperTransportMapper;
import com.screen.screendisplay.service.coal.CoalProdoperTransportService;
import com.screen.screendisplay.vo.coal.CoalTransportVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 * 生产经营煤炭运输情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class CoalProdoperTransportServiceImpl extends ServiceImpl<CoalProdoperTransportMapper, CoalProdoperTransport> implements CoalProdoperTransportService {

    @Override
    public List<CoalTransportVO> getSalesTransport(Integer year) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        
        // 计算6个月前的日期
        LocalDate sixMonthsAgo = currentDate.minusMonths(5);
        
        // 查询最近6个月的数据
        LambdaQueryWrapper<CoalProdoperTransport> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(CoalProdoperTransport::getYear, sixMonthsAgo.getYear())
               .orderByDesc(CoalProdoperTransport::getYear)
               .orderByDesc(CoalProdoperTransport::getMonth);
        
        List<CoalProdoperTransport> transportList = this.list(wrapper);
        
        // 按年月分组，然后按运输类型聚合
        Map<String, Map<Integer, BigDecimal>> monthlyData = new HashMap<>();
        
        for (CoalProdoperTransport transport : transportList) {
            String yearMonth = transport.getYear() + "-" + transport.getMonth();
            monthlyData.putIfAbsent(yearMonth, new HashMap<>());
            
            // 将吨转换为万吨（除以10000）
            BigDecimal volumeInWanTon = transport.getTransportVolume()
                    .divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP);
            
            monthlyData.get(yearMonth).put(
                    transport.getTransportType() != null ? transport.getTransportType() : 0, 
                    volumeInWanTon
            );
        }
        
        // 转换为VO列表
        List<CoalTransportVO> resultList = new ArrayList<>();
        
        // 生成最近6个月的数据
        for (int i = 0; i < 6; i++) {
            LocalDate monthDate = currentDate.minusMonths(i);
            String yearMonth = monthDate.getYear() + "-" + monthDate.getMonthValue();
            
            CoalTransportVO vo = new CoalTransportVO();
            vo.setYear(monthDate.getYear());
            vo.setMonth(monthDate.getMonthValue());
            
            Map<Integer, BigDecimal> monthData = monthlyData.getOrDefault(yearMonth, new HashMap<>());
            
            // 获取各类型运输量
            vo.setRoadTransport(monthData.getOrDefault(0, BigDecimal.ZERO));
            vo.setBeltTransport(monthData.getOrDefault(1, BigDecimal.ZERO));
            vo.setRailTransport(monthData.getOrDefault(2, BigDecimal.ZERO));
            
            resultList.add(vo);
        }
        
        return resultList;
    }
}
